<script lang="ts" setup>
import type { FieldsType } from '../../types';

import type { VxeGridProps } from '#/adapter/vxe-table';

import { useVbenModal } from '@vben/common-ui';

import {
  Button,
  message,
  Modal,
  Popconfirm,
  Space,
  Switch,
  Table,
} from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  deleteGuestbookMessages,
  getGuestbookMessages,
  putGuestbookMessagesStatus,
} from '#/api/plugin/guestbook';
import Icon from '#/components/Icons';
import { toggleStatus } from '#/utils';

const isUpdata = ref(false);
const fields: any = ref([]);
const modalID = ref();
const [TableModal, modalApi] = useVbenModal({
  onBeforeClose: () => {
    modalApi.setData({
      isUpdata: isUpdata.value,
    });
  },
});

const gridOptions: VxeGridProps<FieldsType> = {
  rowConfig: {
    useKey: true,
    height: 120,
  },
  columnConfig: {
    useKey: true,
  },
  columns: [
    { field: 'data', title: '留言内容', slots: { default: 'data' } },
    {
      field: 'status',
      title: '状态',
      slots: { default: 'status' },
      width: 120,
    },
    {
      field: 'update_time',
      title: '留言时间',
      formatter: 'formatShortDateTime',
      width: 150,
    },
    {
      slots: { default: 'action' },
      field: 'action',
      fixed: 'right',
      title: '操作',
      width: 180,
    },
  ],
  exportConfig: {},
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        const modalData = modalApi.getData();
        modalID.value = modalData.id;
        const { data } = await getGuestbookMessages({
          page: page.currentPage,
          limit: page.pageSize,
          formid: modalData.id,
        });

        fields.value = Object.keys(data.listItem).map((key) => {
          return {
            dataIndex: key,
            key,
            title: data.listItem[key].title,
          };
        });
        return data;
      },
    },
  },
  toolbarConfig: {
    custom: true,
    // export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
};

const [Grid, GridApi] = useVbenVxeGrid({
  gridOptions,
});

const handleDelete = async (row: FieldsType) => {
  const $table = GridApi.grid;
  if ($table) {
    const { msg } = await deleteGuestbookMessages({ ids: row.id });
    $table.remove(row);
    message.success(msg);
    isUpdata.value = true;
  }
};

const onChange = async (row: FieldsType) => {
  Modal.confirm({
    title: '系统提示',
    content: `确认要${row.status === 0 ? '驳回' : '审核'}吗?`,
    async onOk() {
      try {
        await putGuestbookMessagesStatus({ id: row.id });
        message.success(`已${row.status === 0 ? '驳回' : '审核'}`);
        isUpdata.value = true;
      } catch {
        row.status = toggleStatus(row.status);
      }
    },
    onCancel() {
      row.status = toggleStatus(row.status);
    },
  });
};
</script>
<template>
  <TableModal :footer="false" class="w-full">
    <Grid table-title="留言列表">
      <template #data="{ row }">
        <Table :columns="fields" :data-source="row.details" />
      </template>
      <template #status="{ row }">
        <Switch
          v-model:checked="row.status"
          :checked-value="1"
          :un-checked-value="0"
          checked-children="审核"
          un-checked-children="未审核"
          @change="onChange(row)"
        />
      </template>
      <template #action="{ row }">
        <Space>
          <Popconfirm
            cancel-text="否"
            ok-text="是"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <Button danger type="primary">
              <template #icon>
                <span class="anticon">
                  <Icon icon="ri:delete-bin-line" />
                </span>
              </template>
              删除
            </Button>
          </Popconfirm>
        </Space>
      </template>
    </Grid>
  </TableModal>
</template>
